{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "<a href=\"https://githubtocolab.com/gee-community/geemap/blob/master/docs/notebooks/07_geojson.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open in Colab\"/></a>\n",
    "\n",
    "Uncomment the following line to install [geemap](https://geemap.org) if needed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# !pip install geemap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import geemap\n",
    "import json\n",
    "import os\n",
    "import requests\n",
    "from geemap import geojson_to_ee, ee_to_geojson\n",
    "from ipyleaflet import GeoJSON"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.show_youtube(\"DbK_SRgrCHw\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5",
   "metadata": {},
   "outputs": [],
   "source": [
    "file_path = os.path.abspath(\"../data/us_states.json\")\n",
    "\n",
    "if not os.path.exists(file_path):\n",
    "    url = \"https://github.com/gee-community/geemap/raw/master/examples/data/us_states.json\"\n",
    "    r = requests.get(url)\n",
    "    with open(file_path, \"w\") as f:\n",
    "        f.write(r.content.decode(\"utf-8\"))\n",
    "\n",
    "with open(file_path) as f:\n",
    "    json_data = json.load(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6",
   "metadata": {},
   "outputs": [],
   "source": [
    "json_layer = GeoJSON(\n",
    "    data=json_data,\n",
    "    name=\"US States JSON\",\n",
    "    hover_style={\"fillColor\": \"red\", \"fillOpacity\": 0.5},\n",
    ")\n",
    "Map.add_layer(json_layer)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7",
   "metadata": {},
   "outputs": [],
   "source": [
    "ee_data = geojson_to_ee(json_data)\n",
    "Map.addLayer(ee_data, {}, \"US States EE\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8",
   "metadata": {},
   "outputs": [],
   "source": [
    "json_data_2 = ee_to_geojson(ee_data)\n",
    "json_layer_2 = GeoJSON(\n",
    "    data=json_data_2,\n",
    "    name=\"US States EE JSON\",\n",
    "    hover_style={\"fillColor\": \"red\", \"fillOpacity\": 0.5},\n",
    ")\n",
    "Map.add_layer(json_layer_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9",
   "metadata": {},
   "outputs": [],
   "source": [
    "file_path = os.path.abspath(\"../data/countries.geojson\")\n",
    "\n",
    "if not os.path.exists(file_path):\n",
    "    url = \"https://github.com/gee-community/geemap/raw/master/examples/data/countries.geojson\"\n",
    "    r = requests.get(url)\n",
    "    with open(file_path, \"w\") as f:\n",
    "        f.write(r.content.decode(\"utf-8\"))\n",
    "\n",
    "with open(file_path) as f:\n",
    "    json_data = json.load(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10",
   "metadata": {},
   "outputs": [],
   "source": [
    "json_layer = GeoJSON(\n",
    "    data=json_data,\n",
    "    name=\"Countries\",\n",
    "    hover_style={\"fillColor\": \"red\", \"fillOpacity\": 0.5},\n",
    ")\n",
    "Map.add_layer(json_layer)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11",
   "metadata": {},
   "outputs": [],
   "source": [
    "from ipywidgets import Text, HTML\n",
    "from ipyleaflet import WidgetControl, GeoJSON\n",
    "\n",
    "html1 = HTML(\n",
    "    \"\"\"\n",
    "    <h4>Country</h4>\n",
    "    Hover over a country\n",
    "\"\"\"\n",
    ")\n",
    "html1.layout.margin = \"0px 20px 20px 20px\"\n",
    "control1 = WidgetControl(widget=html1, position=\"bottomright\")\n",
    "Map.add_control(control1)\n",
    "\n",
    "\n",
    "def update_html(feature, **kwargs):\n",
    "    html1.value = \"\"\"\n",
    "        <h4>Country code: <b>{}</b></h4>\n",
    "        Country name: {}\n",
    "    \"\"\".format(\n",
    "        feature[\"properties\"][\"ISO_A2\"], feature[\"properties\"][\"NAME\"]\n",
    "    )\n",
    "\n",
    "\n",
    "json_layer.on_hover(update_html)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
